﻿using System;
using System.Collections;
using System.Collections.Generic;
using GPSTrackingWebsiteManager.ClassManager;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class Clients : ClassManager.SessionManagerPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!Page.IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = "ClientGroupIdSource.Name";
                expression.SetSortOrder("Ascending");
                this.ClientsGroupGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                expression = new GridSortExpression();
                expression.FieldName = "Name";
                expression.SetSortOrder("Ascending");
                this.ClientsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                expression = new GridSortExpression();
                expression.FieldName = "Name";
                expression.SetSortOrder("Ascending");
                this.EmailsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
            }
            
            
        }

        protected void lbClearEmailFilters_Click(object sender, EventArgs e)
        {
            EmailsGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in EmailsGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }
            EmailsGrid.MasterTableView.Rebind();
        }

        protected void lbClearClientsFilters_Click(object sender, EventArgs e)
        {
            ClientsGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in ClientsGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }
            ClientsGrid.MasterTableView.Rebind();
        }

        protected void lbClearClientsGroupsFilters_Click(object sender, EventArgs e)
        {
            ClientsGroupGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in ClientsGroupGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }
            ClientsGroupGrid.MasterTableView.Rebind();
            clearClientsGrid();
        }

        private void clearClientsGrid()
        {
            ClientsGrid.DataSource = new string[] { };
            ClientsGrid.DataBind();
        }

        private void clearEmailGrid()
        {
            EmailsGrid.DataSource = new string[] { };
            EmailsGrid.DataBind();
        }

        private void ResizeGrid()
        {
            decimal Height = Convert.ToDecimal(HiddenFieldHeight.Value) / 2;

            ClientsGrid.Height = Unit.Parse(Convert.ToString(Height));
            ClientsGroupGrid.Height = Unit.Parse(Convert.ToString(Height));
        }

        protected void ClientsGroupGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            GetGroupsByCompanyID();
            clearClientsGrid();
            ResizeGrid();
        }

        private void GetGroupsByCompanyID()
        {
            SessionManager objSessionManager = new SessionManager();
            objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.ClientsGroupsByCompanies> ClientsGroupsByCompaniesEntity = GPSTrackingDBV2.Data.DataRepository.ClientsGroupsByCompaniesProvider.GetByCompanyId(objSessionManager.CompanyIDSelectedComboBox);
            GPSTrackingDBV2.Data.DataRepository.ClientsGroupsByCompaniesProvider.DeepLoad(ClientsGroupsByCompaniesEntity, true, GPSTrackingDBV2.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingDBV2.Entities.ClientsGroups));

            ClientsGroupGrid.DataSource = ClientsGroupsByCompaniesEntity;
        }

        private GPSTrackingDBV2.Entities.Clients Invoke_Entity(Hashtable Entity)
        {
            GPSTrackingDBV2.Entities.Clients ClientEntity = new GPSTrackingDBV2.Entities.Clients();
            ClientEntity.ClientId = int.Parse(Entity["ClientId"].ToString());
            ClientEntity.Name = Entity["Name"].ToString(); //Convert.ToString(Entity[GPSTrackingDBV2.Entities.ClientsColumn.ClientIdSourceName.ToString()]);
            try
            {
                ClientEntity.Longitude = Double.Parse(Entity["Longitude"].ToString()); //Convert.ToDouble(Entity[GPSTrackingDBV2.Entities.ClientsColumn.ClientIdSourceLongitude.ToString()]);
                ClientEntity.Latitude = Double.Parse(Entity["Latitude"].ToString());//Convert.ToDouble(Entity[GPSTrackingDBV2.Entities.ClientsColumn.ClientIdSourceLatitude.ToString()]);
            }
            catch {
                
            }
            ClientEntity.Phone1 = Entity["Phone1"].ToString();
            ClientEntity.Phone2 = Entity["Phone2"].ToString();           

            return ClientEntity;
        }

        private GPSTrackingDBV2.Entities.ClientsByEmail InvokeClientByEmail_Entity(Hashtable Entity)
        {
            GPSTrackingDBV2.Entities.ClientsByEmail ClientByEmailEntity = new GPSTrackingDBV2.Entities.ClientsByEmail();
            
            if(Entity["ClientId"] != null)
                ClientByEmailEntity.ClientId = int.Parse(Entity["ClientId"].ToString());

            if(Entity["EmailId"] != null)
                ClientByEmailEntity.EmailId = int.Parse(Entity["EmailId"].ToString());

            ClientByEmailEntity.Name = Entity["Name"].ToString(); //Convert.ToString(Entity[GPSTrackingDBV2.Entities.ClientsColumn.ClientIdSourceName.ToString()]);
            ClientByEmailEntity.Email = Entity["Email"].ToString();

            return ClientByEmailEntity;
        }

        protected void ClientsGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetClientsByClientGroupID(GetClientGroupIdSelected());
            ResizeGrid();
        }

        protected void ClientsGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            
            try
            {
               
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                
                Entity["ClientId"] = -1;
                GPSTrackingDBV2.Entities.Clients ClientEntity = Invoke_Entity(Entity);

              

                string typeUserTransaction = "";
                string usernameTransaction = "";

                if (getSessionManager().IsUser)
                {
                    typeUserTransaction = "U";
                    usernameTransaction = getSessionManager().Username;
                }
                if (getSessionManager().IsAdmin)
                {
                    typeUserTransaction = "A";
                    usernameTransaction = getSessionManager().AdminUsername;
                }
                if (getSessionManager().IsMaster)
                {
                    typeUserTransaction = "M";
                    usernameTransaction = getSessionManager().MasterUsername;
                }

                GPSTrackingDBV2Custom.Data.DataRepository.ClientsProvider.Insert(getSessionManager().CompanyIDSelectedComboBox, GetClientGroupIdSelected(), ClientEntity.Name, null, null, ClientEntity.Phone1, ClientEntity.Phone2, typeUserTransaction, usernameTransaction);
                
                //tm.Commit();
            }
            catch (Exception ex)
            {
                //tm.Rollback();
                showErrorMessage("No se pudo registrar el cliente.");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);   
            }  
        }

        protected void ClientsGrid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                tm.BeginTransaction();
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.Clients ClientEntity = Invoke_Entity(Entity);
                ClientEntity.OriginalClientId = int.Parse(Entity["ClientId"].ToString());

                GPSTrackingDBV2.Entities.ClientsLog clientsLogEntity;

                clientsLogEntity = new GPSTrackingDBV2.Entities.ClientsLog();

                clientsLogEntity.ClientId = ClientEntity.OriginalClientId;
                

                DateTime date = DateTime.Now;

                string typeUserTransaction = "";
                string usernameTransaction = "";

                if (getSessionManager().IsUser)
                {
                    typeUserTransaction = "U";
                    usernameTransaction = getSessionManager().Username;
                }
                if (getSessionManager().IsAdmin)
                {
                    typeUserTransaction = "A";
                    usernameTransaction = getSessionManager().AdminUsername;
                }
                if (getSessionManager().IsMaster)
                {
                    typeUserTransaction = "M";
                    usernameTransaction = getSessionManager().MasterUsername;
                }

                clientsLogEntity.TransactionDate = date;
                
                clientsLogEntity.Tick = TickManager.TickManager.GetTick(date);
                ClientEntity.LastTick = clientsLogEntity.Tick;

                clientsLogEntity.SyncTransaction = "U";
                clientsLogEntity.Name = ClientEntity.Name;
                clientsLogEntity.Phone1 = ClientEntity.Phone1;
                clientsLogEntity.Phone2 = ClientEntity.Phone2;
                clientsLogEntity.Latitude = ClientEntity.Latitude;
                clientsLogEntity.Longitude = ClientEntity.Longitude;
                clientsLogEntity.CompanyIdTransaction = getSessionManager().CompanyIDSelectedComboBox;
                clientsLogEntity.TypeUserTransaction = typeUserTransaction;
                clientsLogEntity.UsernameTransaction = usernameTransaction;

                GPSTrackingDBV2.Data.DataRepository.ClientsLogProvider.Insert(clientsLogEntity);

                GPSTrackingDBV2.Data.DataRepository.ClientsProvider.Update(tm, ClientEntity);
                tm.Commit();
            }
            catch (Exception ex)
            {
                tm.Rollback();
                showErrorMessage("No se pudo actualizar el cliente.");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);   
            }
        }

        protected void ClientsGrid_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                tm.BeginTransaction();
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.Clients ClientEntity = Invoke_Entity(Entity);

                GPSTrackingDBV2.Entities.ClientsByClientsGroups ClientsByClientsGroupsEntity = ClassManager.InvokeEntityManager.Invoke_ClientsByClientsGroups_Entity(e);

                GPSTrackingDBV2.Data.DataRepository.ClientsByClientsGroupsProvider.Delete(tm, GetClientGroupIdSelected(), int.Parse(Entity["ClientId"].ToString()));

                GPSTrackingDBV2.Entities.ClientsLog clientsLogEntity;

                clientsLogEntity = new GPSTrackingDBV2.Entities.ClientsLog();

                DateTime date = DateTime.Now;

                string typeUserTransaction = "";
                string usernameTransaction = "";

                if (getSessionManager().IsUser)
                {
                    typeUserTransaction = "U";
                    usernameTransaction = getSessionManager().Username;
                }
                if (getSessionManager().IsAdmin)
                {
                    typeUserTransaction = "A";
                    usernameTransaction = getSessionManager().AdminUsername;
                }
                if (getSessionManager().IsMaster)
                {
                    typeUserTransaction = "M";
                    usernameTransaction = getSessionManager().MasterUsername;
                }

                clientsLogEntity.TransactionDate = date;
                clientsLogEntity.ClientId = int.Parse(Entity["ClientId"].ToString());

                clientsLogEntity.Tick = TickManager.TickManager.GetTick(date);

                clientsLogEntity.SyncTransaction = "D";
                clientsLogEntity.Name = ClientEntity.Name;
                clientsLogEntity.Phone1 = ClientEntity.Phone1;
                clientsLogEntity.Phone2 = ClientEntity.Phone2;                
                clientsLogEntity.Latitude = ClientEntity.Latitude;
                clientsLogEntity.Longitude = ClientEntity.Longitude;
                clientsLogEntity.CompanyIdTransaction = getSessionManager().CompanyIDSelectedComboBox;
                clientsLogEntity.TypeUserTransaction = typeUserTransaction;
                clientsLogEntity.UsernameTransaction = usernameTransaction;

                GPSTrackingDBV2.Data.DataRepository.ClientsLogProvider.Insert(tm, clientsLogEntity);

                GPSTrackingDBV2.Data.DataRepository.ClientsProvider.Delete(tm, int.Parse(Entity["ClientId"].ToString()));
                tm.Commit();
            }
            catch (Exception ex)
            {
                tm.Rollback();
                showErrorMessage("No se pudo eliminar el cliente.");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);   
            }
        }

        protected void ClientsGroupGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetClientsByClientGroupID(GetClientGroupIdSelected());
            ClientsGrid.DataBind();
            ResizeGrid();
        }

        private int GetClientGroupIdSelected() {
            
            foreach (GridDataItem selectedItem in ClientsGroupGrid.SelectedItems)
            {
                return int.Parse(selectedItem["ClientGroupId"].Text); 
            }
            return -1;   
        }

        private int GetClientIdSelected()
        {

            foreach (GridDataItem selectedItem in ClientsGrid.SelectedItems)
            {
                return int.Parse(selectedItem["ClientId"].Text);
            }
            return -1;
        }

        private void GetClientsByClientGroupID(int clientGroupID)
        {

            //GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.ClientsByClientsGroups> ClientsByClientsGroupsEntity = GPSTrackingDBV2.Data.DataRepository.ClientsByClientsGroupsProvider.GetByClientGroupId(clientGroupID);
            //GPSTrackingDBV2.Data.DataRepository.ClientsByClientsGroupsProvider.DeepLoad(ClientsByClientsGroupsEntity, true, GPSTrackingDBV2.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingDBV2.Entities.Clients));

            ClientsGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.ClientsProvider.GetAllByCompanyIDAndClientGroupID(getSessionManager().CompanyIDSelectedComboBox, GetClientGroupIdSelected());
        }

        protected void setSessionManager(string value)
        {
            SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
            objSessionManager.CompaniesByMaster_companySearch_MasterUsername = value;
            HttpContext.Current.Session["SessionManager"] = objSessionManager;
        }

        protected SessionManager getSessionManager()
        {
            return (SessionManager)HttpContext.Current.Session["SessionManager"];
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Error", "objWindowsMessageManager.show_Error('" + msg + "','Administracion de Clientes...');", true);
        }

        public void showInfoMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Info", "objWindowsMessageManager.show_Info('" + msg + "','Administracion de Clientes...');", true);
        }

        public void showWarningMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Warning", "objWindowsMessageManager.show_Warning('" + msg + "','Administracion de Clientes...');", true);
        }

        protected void EmailsGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                tm.BeginTransaction();
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.ClientsByEmail clientByEmailEntity = InvokeClientByEmail_Entity(Entity);

                GPSTrackingDBV2.Entities.ClientsByEmailLog clientByEmailLogEntity;

                clientByEmailLogEntity = new GPSTrackingDBV2.Entities.ClientsByEmailLog();

                DateTime date = DateTime.Now;

                string typeUserTransaction = "";
                string usernameTransaction = "";

                if (getSessionManager().IsUser)
                {
                    typeUserTransaction = "U";
                    usernameTransaction = getSessionManager().Username;
                }
                if (getSessionManager().IsAdmin)
                {
                    typeUserTransaction = "A";
                    usernameTransaction = getSessionManager().AdminUsername;
                }
                if (getSessionManager().IsMaster)
                {
                    typeUserTransaction = "M";
                    usernameTransaction = getSessionManager().MasterUsername;
                }

                clientByEmailLogEntity.TransactionDate = date;
                clientByEmailLogEntity.ClientId = clientByEmailEntity.ClientId;
                clientByEmailLogEntity.EmailId = clientByEmailEntity.EmailId;

                clientByEmailLogEntity.Tick = TickManager.TickManager.GetTick(date);

                clientByEmailLogEntity.SyncTransaction = "D";
                clientByEmailLogEntity.Name = clientByEmailEntity.Name;
                clientByEmailLogEntity.Email = clientByEmailEntity.Email;

                clientByEmailLogEntity.CompanyIdTransaction = getSessionManager().CompanyIDSelectedComboBox;
                clientByEmailLogEntity.TypeUserTransaction = typeUserTransaction;
                clientByEmailLogEntity.UsernameTransaction = usernameTransaction;

                GPSTrackingDBV2.Data.DataRepository.ClientsByEmailLogProvider.Insert(tm, clientByEmailLogEntity);

                GPSTrackingDBV2.Data.DataRepository.ClientsByEmailProvider.Delete(tm, clientByEmailEntity.ClientId, clientByEmailEntity.EmailId);
                tm.Commit();
            }
            catch (Exception ex)
            {
                tm.Rollback();
                showErrorMessage("No se pudo eliminar el correo del cliente.");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
            }
        }

        protected void EmailsGrid_InsertCommand(object sender, GridCommandEventArgs e)
        {
            try
            {

                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);


                Entity["ClientId"] = -1;
                GPSTrackingDBV2.Entities.ClientsByEmail clientByEmailEntity = InvokeClientByEmail_Entity(Entity);



                string typeUserTransaction = "";
                string usernameTransaction = "";

                if (getSessionManager().IsUser)
                {
                    typeUserTransaction = "U";
                    usernameTransaction = getSessionManager().Username;
                }
                if (getSessionManager().IsAdmin)
                {
                    typeUserTransaction = "A";
                    usernameTransaction = getSessionManager().AdminUsername;
                }
                if (getSessionManager().IsMaster)
                {
                    typeUserTransaction = "M";
                    usernameTransaction = getSessionManager().MasterUsername;
                }

                GPSTrackingDBV2Custom.Data.DataRepository.ClientsByEmailProvider.Insert(getSessionManager().CompanyIDSelectedComboBox, GetClientIdSelected(), clientByEmailEntity.Name, clientByEmailEntity.Email, typeUserTransaction, usernameTransaction);

                //tm.Commit();
            }
            catch (Exception ex)
            {
                //tm.Rollback();
                showErrorMessage("No se pudo registrar el correo del cliente.");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
            }
        }

        protected void EmailsGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                tm.BeginTransaction();
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.ClientsByEmail clientByEmailEntity = InvokeClientByEmail_Entity(Entity);
                clientByEmailEntity.OriginalClientId = clientByEmailEntity.ClientId;
                clientByEmailEntity.OriginalEmailId = clientByEmailEntity.EmailId;

                GPSTrackingDBV2.Entities.ClientsByEmailLog clientsByEmailLogEntity;

                clientsByEmailLogEntity = new GPSTrackingDBV2.Entities.ClientsByEmailLog();

                clientsByEmailLogEntity.ClientId = clientByEmailEntity.OriginalClientId;
                clientsByEmailLogEntity.EmailId = clientByEmailEntity.OriginalEmailId;


                DateTime date = DateTime.Now;

                string typeUserTransaction = "";
                string usernameTransaction = "";

                if (getSessionManager().IsUser)
                {
                    typeUserTransaction = "U";
                    usernameTransaction = getSessionManager().Username;
                }
                if (getSessionManager().IsAdmin)
                {
                    typeUserTransaction = "A";
                    usernameTransaction = getSessionManager().AdminUsername;
                }
                if (getSessionManager().IsMaster)
                {
                    typeUserTransaction = "M";
                    usernameTransaction = getSessionManager().MasterUsername;
                }

                clientsByEmailLogEntity.TransactionDate = date;

                clientsByEmailLogEntity.Tick = TickManager.TickManager.GetTick(date);
                clientByEmailEntity.LastTick = clientsByEmailLogEntity.Tick;

                clientsByEmailLogEntity.SyncTransaction = "U";
                clientsByEmailLogEntity.Name = clientByEmailEntity.Name;
                clientsByEmailLogEntity.Email = clientByEmailEntity.Email;
                clientsByEmailLogEntity.CompanyIdTransaction = getSessionManager().CompanyIDSelectedComboBox;
                clientsByEmailLogEntity.TypeUserTransaction = typeUserTransaction;
                clientsByEmailLogEntity.UsernameTransaction = usernameTransaction;

                GPSTrackingDBV2.Data.DataRepository.ClientsByEmailLogProvider.Insert(clientsByEmailLogEntity);

                GPSTrackingDBV2.Data.DataRepository.ClientsByEmailProvider.Update(tm, clientByEmailEntity);
                tm.Commit();
            }
            catch (Exception ex)
            {
                tm.Rollback();
                showErrorMessage("No se pudo actualizar correo del cliente.");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
            }
        }

        protected void EmailsGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            GetEmailByClientID(GetClientIdSelected());
            ResizeGrid();
        }

        private void GetEmailByClientID(int clientId)
        {
            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.ClientsByEmail> clientsByEmailEntity = GPSTrackingDBV2.Data.DataRepository.ClientsByEmailProvider.GetByClientId(clientId);

            EmailsGrid.DataSource = clientsByEmailEntity;
        }

        protected void ClientsGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetEmailByClientID(GetClientIdSelected());
            EmailsGrid.DataBind();
            ResizeGrid();
        }

        private void clearClientSelectedGrid()
        {
            ClientsGrid.DataSource = new string[] { };
            ClientsGrid.DataBind();
            ResizeGrid();
        }

        protected void ClientsGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "AddEmails":

                    if (GetClientIdSelected() == -1)
                    {
                        showWarningMessage("Debe seleccionar un cliente para el registro de los correos.");
                        clearEmailGrid();
                        e.Canceled = true;
                    }
                    break;
            }

            ResizeGrid();
        }

        protected void ClientsGroupGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.SortCommandName || e.CommandName == RadGrid.FilterCommandName)
            {
                clearClientSelectedGrid();
            }
            ResizeGrid();
        }


    }
}